package org.dromara.mqtt.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.mqtt.domain.bo.DeviceControlBoPara;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.mqtt.domain.vo.DeviceControlVo;
import org.dromara.mqtt.domain.bo.DeviceControlBo;
import org.dromara.mqtt.service.IDeviceControlService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * IoT设备控制
 *
 * @author Miles Meng
 * @date 2025-09-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/device/control")
public class DeviceControlController extends BaseController {

    private final IDeviceControlService deviceControlService;

    /**
     * 查询设备控制列表
     */
    @SaCheckPermission("iot:device:control:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceControlVo> list(DeviceControlBo bo, PageQuery pageQuery) {
        return deviceControlService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取设备控制详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("iot:device:control:query")
    @GetMapping("/{id}")
    public R<DeviceControlVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(deviceControlService.queryById(id));
    }

    /**
     * 新增设备控制
     */
    @SaCheckPermission("iot:device:control:add")
    @Log(title = "设备控制", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceControlBo bo) {
        return toAjax(deviceControlService.insertByBo(bo));
    }

    /**
     * 修改设备控制
     */
    @SaCheckPermission("iot:device:control:edit")
    @Log(title = "设备控制", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceControlBo bo) {
        return toAjax(deviceControlService.updateByBo(bo));
    }

    /**
     * 删除设备控制
     *
     * @param ids 主键串
     */
    @SaCheckPermission("iot:device:control:remove")
    @Log(title = "设备控制", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(deviceControlService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 控制设备电源开关
     */
//    @SaCheckPermission("iot:device:control:power")
    @SaIgnore
    @Log(title = "设备控制-开关", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/power")
    public R<Void> setPowerSwitch(@RequestBody DeviceControlBoPara bo) {
        return toAjax(deviceControlService.setPowerSwitch(bo));
    }

    /**
     * 设置设备温度
     */
//    @SaCheckPermission("iot:device:control:temperature")
    @SaIgnore
    @Log(title = "设备控制-温度", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/temperature")
    public R<Void> setTemperature(@RequestBody DeviceControlBoPara bo) {
        return toAjax(deviceControlService.setTemperature(bo));
    }

    /**
     * 设置设备档位
     */
//    @SaCheckPermission("iot:device:control:mode")
    @SaIgnore
    @Log(title = "设备控制-档位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/gear")
    public R<Void> setGear(@RequestBody DeviceControlBoPara bo) {
        return toAjax(deviceControlService.setGear(bo));
    }

    /**
     * 设置设备定时器
     */
//    @SaCheckPermission("iot:device:control:timer")
    @SaIgnore
    @Log(title = "设备控制-定时器", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/timer")
    public R<Void> setTimer(@RequestBody DeviceControlBoPara bo) {
        return toAjax(deviceControlService.setTimer(bo));
    }

    /**
     * 查询设备状态
     */
    @SaCheckPermission("iot:device:control:query")
    @GetMapping("/{deviceId}/status")
    public R<DeviceControlVo> getDeviceStatus(@NotBlank(message = "设备ID不能为空")
                                              @PathVariable String deviceId) {
        return R.ok(deviceControlService.getDeviceStatus(deviceId));
    }
}
